<template>
  <el-dialog
    top="10vh"
    width="500px"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  data() {
    return {
      dialogValue: false,
      ruleForm: {
      },
      defaultForm: {
        name: '',
        packageId: '',
        contactName: '',
        contactMobile: '',
        username: '',
        password: '',
        accountCount: '',
        expireTime: '',
        website: '',
        status: ''
      },
      rules: {
        name: [{ required: true, message: '不能为空' }],
        packageId: [{ required: true, message: '不能为空' }],
        contactName: [{ required: true, message: '不能为空' }],
        contactMobile: [{ required: true, message: '不能为空' }],
        username: [{ required: true, message: '不能为空' }],
        password: [{ required: true, message: '不能为空' }],
        accountCount: [{ required: true, message: '不能为空' }],
        expireTime: [{ required: true, message: '不能为空' }],
        website: [{ required: true, message: '不能为空' }],
        status: [{ required: true, message: '不能为空' }]
      },
      formList: [
        {
          prop: 'name',
          name: 'input',
          label: '名称',
          colSpan: 12
        },
        {
          prop: 'packageId',
          name: 'select',
          label: '租户套餐',
          options: [],
          colSpan: 12
        },
        {
          prop: 'contactName',
          name: 'input',
          label: '联系人',
          colSpan: 12
        },
        {
          prop: 'contactMobile',
          name: 'input',
          label: '联系手机',
          colSpan: 12
        },
        {
          prop: 'username',
          name: 'input',
          label: '用户名称',
          colSpan: 12
        },
        {
          prop: 'password',
          name: 'input',
          label: '用户密码',
          colSpan: 12
        },
        {
          prop: 'accountCount',
          name: 'input',
          label: '账号额度',
          colSpan: 12
        },
        {
          prop: 'expireTime',
          name: 'date',
          label: '过期时间',
          colSpan: 12
        },
        {
          prop: 'website',
          name: 'input',
          label: '绑定域名',
          colSpan: 24
        },
        {
          prop: 'status',
          name: 'radio',
          label: '租户状态',
          options: [
            {
              value: 0,
              label: '启用'
            },
            {
              value: 1,
              label: '禁用'
            }
          ],
        }
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
      http.system.tenantPackage.simpleList().then(res => {
        this.formList[1].options = res.data.map(item => {
          return {
            value: item.id,
            label: item.name
          }
        })
      })
    },
    onSave() {
      http.system.tenant[this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
